﻿using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using IdentityModel.Client;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MvcApp.Models;
using Newtonsoft.Json.Linq;

namespace MvcApp.Controllers
{
  public class HomeController : Controller
  {
    public IActionResult Index()
    {
      return View();
    }

    public IActionResult About()
    {
      ViewData["Message"] = "Your application description page.";

      return View();
    }

    public IActionResult Contact()
    {
      ViewData["Message"] = "Your contact page.";

      return View();
    }

    public IActionResult Privacy()
    {
      return View();
    }

    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
      return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }

    [Authorize]
    public IActionResult Secure()
    {
      ViewData["Message"] = "Secure page.";

      return View();
    }

    public async Task Logout()
    {
      await HttpContext.SignOutAsync("Cookies");
      await HttpContext.SignOutAsync("oidc");
    }


    public async Task<IActionResult> CallApiUsingClientCredentials()
    {
      var tokenClient = new TokenClient("http://localhost:5500/connect/token", "mvc", "secret");
      var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api3");

      var client = new HttpClient();
      client.SetBearerToken(tokenResponse.AccessToken);
      var content = await client.GetStringAsync("http://localhost:5501/api/identity");

      ViewBag.Json = JArray.Parse(content).ToString();
      return View("json");
    }

    public async Task<IActionResult> CallApiUsingUserAccessToken()
    {
      var accessToken = await HttpContext.GetTokenAsync("access_token");

      var client = new HttpClient();
      client.SetBearerToken(accessToken);
      var content = await client.GetStringAsync("http://localhost:5501/api/identity");

      ViewBag.Json = JArray.Parse(content).ToString();
      return View("Json");
    }
  }
}
